package com.antoco.operation.bigscreen.service;

import com.antoco.operation.bigscreen.repository.UserRepository;
import com.antoco.operation.bigscreen.model.entity.User;
import com.antoco.operation.bigscreen.third.jwt.JwtUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author guozy
 * @date 2018-11-22
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class JwtUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username){

        User user = new User();
        user.setId("123456");
        user.setName("name");
        user.setCode("code");
        user.setPassword("pwd");

        return new JwtUser(user);
    }

    // private static List<GrantedAuthority> mapToGrantedAuthorities(Set<Role> roles, PermissionRepository permissionRepository) {

    //     Set<Permission> permissions = new HashSet<>();
    //     for (Role role : roles) {
    //         Set<Role> roleSet = new HashSet<>();
    //         roleSet.add(role);
    //         permissions.addAll(permissionRepository.findByRoles_Id(role.getId()));
    //     }

    //     return permissions.stream()
    //             .map(permission -> new SimpleGrantedAuthority(permission.getName()))
    //             .collect(Collectors.toList());
    // }
}
